package pe.com.bbva.mantenimiento.dao.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pe.com.bbva.mantenimiento.dao.ParametroDAO;
import pe.com.bbva.mantenimiento.domain.Parametro;
import pe.com.stefanini.core.dao.GenericDAOImpl;
import pe.com.stefanini.core.exceptions.BOException;
import pe.com.stefanini.core.exceptions.DAOException;
import pe.com.stefanini.core.util.StringUtil;

@Service("parametroDAO")
public class ParametroDAOImpl extends GenericDAOImpl<Parametro> implements ParametroDAO{
	Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	public ParametroDAOImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	public List<Parametro> findParametros(Parametro parametro)throws BOException, DAOException {
		String where = " where upper(descripcion) like upper('%" +
						(parametro.getDescripcion()== null?"":parametro.getDescripcion())+
						"%')";
		if(parametro.getCodigo()!= null &&
		   !parametro.getCodigo().equals("")){
			where = where +" and upper(codigo) like upper('%" +
					parametro.getCodigo()+
					"%') ";
		}
		if(parametro.getValor()!= null &&
				   !parametro.getValor().equals("")){
					where = where +" and upper(valor) like upper('%" +
							parametro.getValor()+
							"%') ";
		}
		
		if(parametro.getEstado()!= null &&
				   !parametro.getEstado().equals("")){
					where = where +" and upper(estado) like upper('%" +
							parametro.getEstado()+
							"%') ";
		}
		
		String orders = " order by fechaCreacion desc,codigo, descripcion";
		List<Parametro> listaParametros = super.executeQuery(Parametro.class,where,orders);
		return listaParametros;
	}
	
	@SuppressWarnings("deprecation")
	public Long ValidaCargaEnProceso() throws DAOException {
		Session hibernateSession = null;
		Connection con = null;
		CallableStatement cstmt =null;
		ResultSet rs  = null;		
		Long IdCarga=0L;
		try {
			hibernateSession = (Session) getHibernateTemplate().getSessionFactory().openSession();
			con = hibernateSession.connection();
			cstmt =con.prepareCall("{call TARGPS.TARGPSPKG.PR_TARGPS_VALIDAR_ENPROCESO(?)}");			
			cstmt.registerOutParameter(1,oracle.jdbc.OracleTypes.VARCHAR);		
			cstmt.executeQuery();			
			IdCarga = Long.parseLong((String)cstmt.getObject(1));			
		} catch (HibernateException e) {
			logger.error(StringUtil.getStackTrace(e));
			throw new DAOException(e.getMessage());
		} catch (SQLException e) {
			logger.error(StringUtil.getStackTrace(e));
			throw new DAOException(e.getMessage());
		} catch (Exception e) {
			logger.error(StringUtil.getStackTrace(e));
			throw new DAOException(e.getMessage());
		}finally{
			try {
				if(rs!=null){
					rs.close();
				}
				if(cstmt!=null){
					cstmt.close();
				}
				hibernateSession.close();
			} catch (SQLException e) {
				throw new DAOException(e.getMessage());
			} catch (HibernateException e) {
				logger.error(StringUtil.getStackTrace(e));
				throw new DAOException(e.getMessage());
			} 
		}
		return IdCarga;
	}
}